<?php

/*
 * This class is used for as Model for Request table 
 */

class Model_Requestcomment extends Model_Base {

    protected $_name = 'tbl_request_comment';
    protected $_primaryKey = 'cmnt_id';

   
   
   
   
      public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('request' => 'tbl_request_comment'))
                ->setIntegrityCheck(false)
                ->where('cmnt_id = ?', $id)
                ->where('cmnt_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get requests
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchRequestComment($where = "", $limit = "", $offset = "0", $order = 'cmnt_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('request' => 'tbl_request_comment'))
                ->where('cmnt_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        $finalArray = array();
        foreach($data as $key =>$value){
           if($value['cmnt_comment_by'] == 1){
             //admin
             $adminModel = new Model_Admin();
             $admin_data = $adminModel ->fetchEntryById($value['adm_id']);
             $added_by = 'Admin';
             $name = $admin_data['adm_firstname']." ". $admin_data['adm_lastname'];
           }if($value['cmnt_comment_by'] == 2){
             //contractor
             $contractorModel = new Model_Contractor();
             $contractor_data = $contractorModel ->fetchEntryById($value['cont_id']);
             $added_by = 'Contractor';
             $name = $contractor_data['cont_firstname']." ". $contractor_data['cont_lastname'];
           }if($value['cmnt_comment_by'] == 3){
             //customer
             $customerModel = new Model_Customer();
             $customer_data = $customerModel ->fetchEntryById($value['cust_id']);
             $added_by = 'Customer';
             $name = $customer_data['cust_firstname']." ". $customer_data['cust_lastname'];
           } 
           
           $finalArray[$key]['Addedby'] = $added_by; 
           $finalArray[$key]['Name'] = $name; 
           $finalArray[$key]['comment'] = $value['cmnt_comment']; 
           $finalArray[$key]['cmnt_add_attachment'] = $value['cmnt_add_attachment']; 
           $finalArray[$key]['createDate'] = $value['cmnt_createddate']; 
           $finalArray[$key]['Status'] = $value['cmnt_status']; 
        }
         return $finalArray;
        /*
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }*/
    }

    

   
}
